package com.xuan.dao;
import com.xuan.model.Game;
import org.apache.commons.dbutils.ResultSetHandler;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.LinkedList;
import java.util.List;
public class GameDao extends BaseDao{
    public boolean add (Game game) {
        String sql = "insert into game (id,icon,name) values ((select max(id) from game)+1,?,?)";
        try {
            return queryRunner.execute(sql,
                    game.getIcon(),
                    game.getTitle()) == 1;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false;
    }

    public boolean delete (int id) {
        String sql = "delete from game where id = ?";
        try {
            return queryRunner.execute(sql,id) == 1;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false;
    }

    public boolean update (Game game) {
        String sql = "update game set icon = ?,name = ?  where id =?";
        Game game1 = get(game.getId());
        if (game.getIcon() == null && game1.getIcon() != null) {
            game.setIcon(game1.getIcon());
        }
        try {
            return queryRunner.execute(sql,game.getIcon(),
                    game.getTitle(),
                    game.getId()) == 1;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false;
    }

    public List<Game> getAll (int size,int page) {
        List<Game> list = new LinkedList<>();
        String sql =  "select id, icon, name from game where" +
                " id not in (select id from game where rownum <= ?) and rownum <= ?";
        try {
            return queryRunner.query(sql, new ResultSetHandler<List<Game>>() {
                @Override
                public List<Game> handle(ResultSet rs) throws SQLException {
                    while (rs.next()) {
                        list.add(new Game(rs.getInt("id"),
                                rs.getString("icon"),
                                rs.getString("name")));
                    }
                    return list;
                }
            },(page - 1) * size,size);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }

    public List<Game> getAll () {
        List<Game> list = new LinkedList<>();
        String sql =  "select id, icon, name from game";
        try {
            return queryRunner.query(sql, new ResultSetHandler<List<Game>>() {
                @Override
                public List<Game> handle(ResultSet rs) throws SQLException {
                    while (rs.next()) {
                        list.add(new Game(rs.getInt("id"),
                                rs.getString("icon"),
                                rs.getString("name")));
                    }
                    return list;
                }
            });
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }

    public Game get (String name) {
        String sql = "select id, icon, name from game where name = ?";
        try {
            return queryRunner.query(sql,new BeanHandler<>(Game.class),name);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    public long getPageSize(int size) {
        String sql = "select count(1) from game";
        try {
            double res = Double.valueOf(queryRunner.query(sql,new ScalarHandler<>()).toString());
            return (long)Math.ceil(res/size);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    public Game get (int id) {
        String sql = "select id,icon,name from game where id = ?";
        try {
            return queryRunner.query(sql,new BeanHandler<>(Game.class),id);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }
}
